<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="合同编号" prop="username">
        <el-input v-model="drawerProps.row!.username" placeholder="请填写合同编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="承包商" prop="gender">
        <el-select
          v-model="drawerProps.row!.gender"
          placeholder="请选择承包商"
          clearable
          filterable
          @change="handleContractorChange"
        >
          <el-option
            v-for="item in getGenderType()"
            :key="item.genderValue"
            :label="item.genderLabel"
            :value="item.genderValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开户行" prop="kaihuname">
        <el-input v-model="drawerProps.row!.kaihuname" disabled placeholder="请选择开户行" clearable></el-input>
      </el-form-item>
      <el-form-item label="银行账号" prop="backname">
        <el-input v-model="drawerProps.row!.backname" disabled placeholder="请填写银行账号" clearable></el-input>
      </el-form-item>
      <el-form-item label="合同金额" prop="hetongjine">
        <el-input v-model="drawerProps.row!.hetongjine" placeholder="请填写合同金额" clearable></el-input>
      </el-form-item>
      <el-form-item label="付款总额" prop="fukuanzonge">
        <el-input v-model="drawerProps.row!.fukuanzonge" placeholder="请填写付款总额" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import { getGenderType } from "@/utils/dict";

const rules = reactive({
  // username: [{ required: true, message: "请填写合同编号" }],
  gender: [{ required: true, message: "请选择承包商" }],
  kaihuStut: [{ required: true, message: "请选择开户行" }],
  email: [{ required: true, message: "请填写银行账号" }],
  hetongjine: [{ required: true, message: "请填写合同金额" }],
  fukuanzonge: [{ required: true, message: "请填写付款总额" }]
});
interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

// 承包商方法定义
const handleContractorChange = selectedValue => {
  // 找到选中的承包商
  const selectedContractor = getGenderType().find(item => item.genderValue === selectedValue);
  console.log("selectedContractor", selectedContractor);
  // 如果找到了承包商且它有开户行信息，则赋值
  if (selectedContractor) {
    // 自动填充开户户名
    drawerProps.value.row!.kaihuname = selectedContractor.kaihuname;
    // 自动填充银行账号
    drawerProps.value.row!.backname = selectedContractor.backname || "";
  } else {
    drawerProps.value.row!.kaihuname = ""; // 清空或保持原样
    drawerProps.value.row!.backname = ""; // 清空或保持原样
  }
};

defineExpose({
  acceptParams
});
</script>
